package com.me.forum.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.forum.dao.IImageDao;
import com.me.forum.domain.Category;
import com.me.forum.domain.Image;
import com.me.forum.request.ImageRequest;
import com.me.forum.service.IImageService;
import org.springframework.stereotype.Service;

@Service
public class ImageService extends BaseService<IImageDao, Image> implements IImageService {
    @Override
    public IPage<Image> getImage(ImageRequest ireq, String page, String size) {
        return dao.selectByAll(ireq,new Page<Image>(Long.valueOf(size),Long.valueOf(size)));
    }

    @Override
    public Image addImage(ImageRequest ireq) {
        if (!ireq.isLegal()) {
            return null;
        }

        Image image = dao.selectByUserId(ireq.getUserId());
        if (image != null) {
            return null;
        }

        int saveNum = dao.insertSelective(ireq);
        return saveNum == 1? dao.selectByUserId(ireq.getUserId()) : null;
    }

    @Override
    public boolean updateByid(ImageRequest ireq) {
        if (!ireq.isLegal()) {
            return false;
        }
        return dao.updateByid(ireq) == 1;
    }
}
